<%@ page import="com.xiaolin.utils.dbUtils" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="com.xiaolin.dao.impl.CartDaoImpl" %>
<%@ page import="com.xiaolin.bean.Cart" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.DecimalFormat" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/1/7
  Time: 0:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="../css/cart.css" type="text/css">
</head>
<body>
<div class="div1"><h1><%=request.getParameter("name")%>的购物车</h1></div>
<div class="div2">
    <table cellspacing="0px" cellpadding="10px">
        <tr>
            <td>商品名称</td>
            <td>商品数量</td>
            <td>商品单价</td>
            <td>商品总价</td>
            <td></td>
        </tr>

        <%
            int id = Integer.parseInt(request.getParameter("id"));
            if (id != 0) {
                Connection conn = dbUtils.getConnection();
                CartDaoImpl cartDao = new CartDaoImpl();
                String user = request.getParameter("name");
                cartDao.deleteById(conn, id, user);
            }
        %>

        <%
            String name = request.getParameter("name");
            Connection conn = dbUtils.getConnection();
            CartDaoImpl cartDao = new CartDaoImpl();
            List<Cart> all = cartDao.getAll(conn, name);
            for (int i = 0; i < all.size(); i++) {
                Cart cart = all.get(i);
                int num = cart.getNum();
                DecimalFormat decimalFormat = new DecimalFormat(".00");
                double price = cart.getPrice();
                String format = decimalFormat.format(price);
                double a = num * price;
                String format1 = decimalFormat.format(a);
        %>
        <tr>
            <td><%=cart.getName()%>
            </td>
            <td><%=num%>
            </td>
            <td><%=format%>
            </td>
            <td><%=format1%>
            </td>
            <td><a href="cart.jsp?name=<%=name%>&id=<%=cart.getId()%>">删除</a></td>
        </tr>
        <%
            }
        %>
    </table>
</div>
</body>
</html>
